from flask import Blueprint,jsonify,request,abort
from flask_restful import Api,Resource
from app.models import *
# main = Blueprint("main", __name__)
enterprise0= Api()


class Enterprise1(Resource):
    def get(self):
        af = Enterprise.query.all()
        return jsonify({"af": list(map(con2, af))})
    def post(self, uid):
        h = Enterprise(
            request.json.get("enterprise_name"),
            request.json.get("contact_person"),
            request.json.get("contact_number"),
            request.json.get("invoice_title"),
            request.json.get("invoice_number"),
            request.json.get("certificate"),
            request.json.get("sector"),
            request.json.get("number_persons"),
            request.json.get("price"),
            request.json.get("feedback"),
            request.json.get("time"),
            request.json.get("remarks")
        )
        db.session.add(h)
        db.session.commit()
        return jsonify({"h": con2(h)}), 201
enterprise0.add_resource(Enterprise1, "/Enterprise1/")


class Enterprise2(Resource):
    def get(self, uid):
        ag = Enterprise.query.filter_by(id=uid).first()
        if ag is None:
            abort(404)
        return jsonify({"ag": con2(ag)})

    def put(self, uid):
        ai = Enterprise.query.get(uid)
        ai.enterprise_name = request.json.get("enterprise_name")
        ai.contact_person = request.json.get("contact_person")
        ai.contact_number = request.json.get("contact_number")
        ai.invoice_title = request.json.get("invoice_title")
        ai.invoice_number = request.json.get("invoice_number")
        ai.certificate = request.json.get("certificate")
        ai.sector = request.json.get("sector")
        ai.number_persons = request.json.get("number_persons")
        ai.price = request.json.get("price")
        ai.feedback = request.json.get("feedback")
        ai.textual_research_time = request.json.get("textual_research_time")
        ai.remarks = request.json.get("remarks")
        db.session.add(ai)
        db.session.commit()
        return jsonify({"ai": con2(ai)})


enterprise0.add_resource(Enterprise2, "/Enterprise2/<int:uid>/")